package com.wumz.netty.tomcat.nio.handler;

import com.wumz.netty.tomcat.nio.http.MyServlet;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by wumz on 2021/3/11 20:30
 */
public class AbstractTomcatHandler extends ChannelInitializer {

    private Map<String, MyServlet> servletMapping = new HashMap<>();

    public AbstractTomcatHandler(Map<String, MyServlet> servletMapping){
        this.servletMapping = servletMapping;
    }

    @Override
    protected void initChannel(Channel channel) throws Exception {

        ChannelPipeline pipeline = channel.pipeline();
        pipeline.addLast(
                // TODO 处理回调的逻辑

                // TODO 链式编程，责任链模式

                // 处理响应结果的封装
                new HttpResponseEncoder(),
                // 解码
                new HttpRequestDecoder(),
                //处理自己的业务逻辑
                new BusinessHandler(servletMapping)
        );
    }
}
